package com.yinhe.music.yhmusic.singer.parse;

import com.yinhe.music.yhmusic.model.DataItem;
import com.yinhe.music.yhmusic.utils.Preferences;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Johnson on 2018/3/9.
 */

public class SingerParseUtil {


    public static final List<SingerGroup> parseSingerData(String data) {

        List<SingerGroup> singerGroups = new ArrayList<>();

        long time = System.currentTimeMillis();

//        data = "004,阿山,A;1085,阿佳组合,B;1097,阿才,C";
        String[] singers = data.split(";");

        SingerGroup group = null;
        for (int i = 0; i < singers.length; i++) {
            String item = singers[i];
            String[] detail = item.split(",");
            if (detail.length == 5) {
                Singer singer = new Singer();
                singer.id = detail[0].trim();
                singer.name = detail[1].trim();
                singer.groupName = detail[2].trim();
                singer.type = Integer.valueOf(detail[3].trim());
                singer.image = detail[4];
                if (singer.groupName.equals("~")) {
                    singer.groupName = "#";
                }

                if (group == null) {
                    group = new SingerGroup();
                    group.groupName = singer.groupName;
                    singerGroups.add(group);
                } else {
                    if (!group.groupName.equals(singer.groupName)) {
                        group = new SingerGroup();
                        group.groupName = singer.groupName;
                        singerGroups.add(group);
                    }
                }
                group.addSinger(singer);
            }
        }

        return singerGroups;
    }

    public static String getSingerImage(String image) {
        return Preferences.getImageHost() + image;
    }

    public static List<DataItem> getSingerByType(int type, List<SingerGroup> singerGroups) {

        if (singerGroups != null && !singerGroups.isEmpty()) {
            List<DataItem> ret = new ArrayList<>();
            if (type == 0) {
                ret.add(new DataItem(DataItem.TYPE_RECENTLY));

                ret.add(new DataItem(DataItem.TYPE_HOT));
            }
            for (int i = 0; i < singerGroups.size(); i++) {
                SingerGroup singerGroup = singerGroups.get(i);
                boolean addGroup = true;
                for (int j = 0; j < singerGroup.singers.size(); j++) {
                    Singer singer = singerGroup.singers.get(j);
                    if (type != 0 && singer.type != type) {
                        continue;
                    }

                    DataItem item = new DataItem(singer);
                    if (j == singerGroup.singers.size() - 1) {
                        item.setBottom(true);
                    }

                    if (addGroup) {
                        ret.add(new DataItem(singerGroup.groupName));
                        addGroup = false;
                    }
                    ret.add(item);
                }
            }
            return ret;
        }

        return null;
    }

}
